package cn.dw.mm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.form.FormProperty;
import org.activiti.engine.form.TaskFormData;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import cn.dw.mm.domain.KhpjForm;

@Controller
@RequestMapping(value = "/activiti/task")
public class TaskController extends ActivitiAbstractController {
	
	private ProcessInstance processInstance;
	private ProcessDefinition processDefinition;
	
	/**
	 * 查询流程进度 
	 */
	@RequestMapping(value = "showProcess")
	@ResponseBody
	public List<HistoricActivityInstance> showProcess() {
		if (processInstance == null) {
			return new ArrayList<>();
		} else {
			List<HistoricActivityInstance> historicActivityInstanceList = historyService.createHistoricActivityInstanceQuery().processInstanceId(processInstance.getId()).list();
			return historicActivityInstanceList;	
		}
	}
	
	/**
	 * 启动任务
	 */
	@RequestMapping(value = "start/{userId}")
	@ResponseBody
	public String start(@RequestBody KhpjForm khpjForm, @PathVariable("userId") String userId) {
		
		List<ProcessDefinition> processDefinitionList = repositoryService.createProcessDefinitionQuery().processDefinitionName("考核评价流程").list();
		if (processDefinitionList.isEmpty()) {
			return "noDeployment";
		}
		
		processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("khpj").singleResult();
		// 设置当前用户
	    String currentUserId = userId;
	    identityService.setAuthenticatedUserId(currentUserId);

	    Map<String, String> variables = formDataToMap(khpjForm);     
	    processInstance = formService.submitStartFormData(processDefinition.getId(), variables);
	      
	    return "true";
	}
	
	/**
	 * 查询任务
	 */
	@RequestMapping(value = "task/{userId}")
	@ResponseBody
	public List<org.activiti.engine.form.FormProperty> task(@PathVariable("userId") String userId) {
		//首先查询当前用户id的待办任务
		Task task = taskService.createTaskQuery().taskAssignee(userId).singleResult();
	    TaskFormData formData = formService.getTaskFormData(task.getId());//查询表单属性值
	    List<FormProperty> formPropsList = formData.getFormProperties();
	    return formPropsList;
	}
	
	/**
	 * 传递任务
	 */
	@RequestMapping(value = "deliverTask/{userId}")
	@ResponseBody
	public String deliverTask(@RequestBody KhpjForm khpjForm, @PathVariable("userId") String userId) {
		//首先查询当前用户id的待办任务
		Task task = taskService.createTaskQuery().taskAssignee(userId).singleResult();
		Map<String, String> variables = formDataToMap(khpjForm);
		formService.submitTaskFormData(task.getId(), variables);
		return "true";
	}
	
	//将传递过来的表单数据包装成Map
	private Map<String, String> formDataToMap(KhpjForm khpjForm) {
		Map<String, String> variables = new HashMap<String, String>();
		variables.put("groupName", khpjForm.getGroupName());
		variables.put("name", khpjForm.getGroupName());
	    variables.put("department", khpjForm.getDepartment());
	    variables.put("project", khpjForm.getProject());
	    variables.put("groupWork", khpjForm.getGroupWork());
	    variables.put("selfComment", khpjForm.getSelfComment());
	    variables.put("inspectionGroupLeader", khpjForm.getInspectionGroupLeader());
	    variables.put("groupComment", khpjForm.getGroupComment());
	    variables.put("leaderComment", khpjForm.getLeaderComment());
	    variables.put("leaderComment2", khpjForm.getLeaderComment2());
	    variables.put("reApply", khpjForm.getReApply());
	    return variables;
	}
	
	
}
